package com.yc.damai.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/**
 *需求： 当登录成功之后， 跳转回打开登录页面之前的页面
	1. 过滤器， 拦截访问登录页面的请求
	2. HTTP请求报文
			Referer: http://localhost/C94-S2-Ply-damai/cart.html
			Referer：涉及 =》
	3. 会话
 */
@WebFilter("/login.html")
public class ToLoginFilter implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		// 获取上一个页面
		String value = req.getHeader("Referer");
		// 保存到会话中
		req.getSession().setAttribute("Referer", value);
		// 跳转到 login.html
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

	@Override
	public void destroy() {
	}

}
